//index.js
//获取应用实例
const app = getApp()
import api from '../../http/api'
import user from '../../utils/user'

let currentPlayInfo = {
  currentPlayContext: null,
  currentIndex: -1
}
Page({
  data: {
    navSet: [{}],
    selectIndex: 0,
    host: app.globalData.HOST_ADDRESS,
    clientConfig: ""
  },
  //事件处理函数
  bindViewTap: function () {

  },
  onLoad: function () {
    this.init();
  },
  init(){
     /**
     * 用户是否存有权限
     */
    user.author("", (res) => {
      console.log(res)
      if (res['scope.userInfo']) {
        /**
         * 更新用户信息
         */
        user.updataUserInfo().then((res) => {
          this.setData({
            clientConfig:res
          })
          this.listCourse(1)
          this.chatInit();
        })
      }else{
        wx.showModal({
          title:"提示",
          content:"您还没有授权呢，快去授一下吧",
          cancelColor: 'cancelColor',
          success:(res)=>{
            this.authorUserInfo()
          }
        })
      }
    })

  },
  onShow(){

  },
  authorUserInfo(){
    let _this =this;
    wx.navigateTo({
      url: '/pages/author/author',
      events: {
        onUserAuthorSuccess: function(data) {
          _this.init()
        },
        success: function(res) {
      
        }
      },
    })
  },
  chatInit() {

  },
  /**
   * 获取课程
   * @param {*} page 
   */
  listCourse(page) {
    api.listTypes().then((res) => {
      let set = new Set();
      for (let item of res.data) {
        set.add({ "name": item, "videoSet": [] })
      }
      const arr = [...set];
      this.setData({
        navSet: arr
      })
      api.listCourse(1, arr[0].name).then((videoRes) => {
        let tempNav = this.data.navSet;
        tempNav[0].videoSet = videoRes.data;
        this.setData({
          navSet: tempNav
        })
      })
    }).catch((res) => {
    })
  },

  onVideoPlay(e) {
    let tempNav = this.data.navSet;

    if (currentPlayInfo.currentPlayContext != null) {
      currentPlayInfo.currentPlayContext.stop();
      tempNav[this.data.selectIndex].videoSet[currentPlayInfo.currentIndex].showControls = false;
    }

    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    var videoContext = wx.createVideoContext(id, this);

    currentPlayInfo.currentPlayContext = videoContext
    currentPlayInfo.currentIndex = index

    videoContext.play();

    tempNav[this.data.selectIndex].videoSet[index].showControls = true;
    this.setData({
      navSet: tempNav,
    })
  },
  onVideoClick(e) {
    let index = e.currentTarget.dataset.index;
    let videoInfo = this.data.navSet[this.data.selectIndex].videoSet[index];
    app.globalData.currentVideoInfo = videoInfo
    wx.navigateTo({
      url: '/pages/video/video',
    })
  },
  onNavClick(e) {
    let tempNav = this.data.navSet;

    if (currentPlayInfo.currentPlayContext != null) {
      currentPlayInfo.currentPlayContext.stop();
      tempNav[this.data.selectIndex].videoSet[currentPlayInfo.currentIndex].showControls = false;
    }
    currentPlayInfo.currentPlayContext = null;
    this.setData({
      navSet: tempNav
    })

    let index = e.currentTarget.dataset.index;
    console.log(index)
    this.setData({
      selectIndex: index
    })
    api.listCourse(1, this.data.navSet[index].name).then((videoRes) => {
      let tempNav = this.data.navSet;
      tempNav[index].videoSet = videoRes.data;
      this.setData({
        navSet: tempNav
      })
    })
  }

})
